package org.graduation.recruitment.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.graduation.recruitment.entity.exception.ServiceException;
import org.graduation.recruitment.entity.pojo.Job;
import org.graduation.recruitment.entity.pojo.ResumeAndJob;
import org.graduation.recruitment.entity.vo.CheckResumeVO;
import org.graduation.recruitment.entity.vo.ResumeVO;
import org.graduation.recruitment.mapper.EnterpriseUserMapper;
import org.graduation.recruitment.mapper.JobMapper;
import org.graduation.recruitment.mapper.ResumeAndJobMapper;
import org.graduation.recruitment.service.ResumeAndJobService;
import org.graduation.recruitment.service.other.InterviewNotification;
import org.graduation.recruitment.service.resume.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResumeAndJobServiceImpl extends ServiceImpl<ResumeAndJobMapper, ResumeAndJob> implements ResumeAndJobService {
    @Autowired
    private ResumeAndJobMapper resumeAndJobMapper;
    @Autowired
    private ResumeService resumeService;
    @Autowired
    private InterviewNotification interviewNotification;
    @Autowired
    private EnterpriseUserMapper enterpriseUserMapper;
    @Autowired
    private JobMapper jobMapper;

    @Override
    public boolean submitResume(ResumeAndJob resumeAndJob) {
        resumeAndJob.setStatus(0);
        try {
            return resumeAndJobMapper.insert(resumeAndJob) == 1;
        } catch (Exception e) {
            log.warn(e.toString());
            throw new ServiceException("已投递此岗位", e);
        }
    }

    @Override
    public boolean pass(ResumeAndJob resumeAndJob) {
        resumeAndJob.setStatus(1);
        boolean result = resumeAndJobMapper.update(passUpdateWrapper(resumeAndJob.getResumeId(), resumeAndJob.getJobId())) == 1;
        return result;
    }

    @Override
    public boolean notPass(ResumeAndJob resumeAndJob) {
        resumeAndJob.setStatus(2);
        return resumeAndJobMapper.update(noPassUpdateWrapper(resumeAndJob.getResumeId(), resumeAndJob.getJobId())) == 1;
    }

    @Override
    public List<ResumeVO> getResumeByJobId(Long jobId) {
        List<Long> resumeIdList = resumeAndJobMapper.selectList(getResumeIdListByJobId(jobId)).stream().map(ResumeAndJob::getResumeId).toList();
        List<ResumeVO> resumeVOList = new ArrayList<>();
        for (Long resumeId : resumeIdList) {
            resumeVOList.add(resumeService.getResumeAndAllResumeModels(resumeId));
        }
        return resumeVOList;
    }

    @Override
    public CheckResumeVO getCheckResumeVO(Long enterpriseId) {
        CheckResumeVO checkResumeVO = new CheckResumeVO();
        checkResumeVO.setEnterprise(enterpriseUserMapper.selectById(enterpriseId));
        Map<Long, List<ResumeVO>> resumeMap = new HashMap<>();
        LambdaQueryWrapper<Job> jobListQuery = new LambdaQueryWrapper<>();
        jobListQuery
                .eq(Job::getEnterpriseId, enterpriseId);
        List<Job> jobList = jobMapper.selectList(jobListQuery);
        for (Job job : jobList) {
            resumeMap.put(job.getId(), getResumeByJobId(job.getId()));
        }
        checkResumeVO.setJobs(jobList);
        checkResumeVO.setResume(resumeMap);
        return checkResumeVO;
    }

    private LambdaUpdateWrapper<ResumeAndJob> passUpdateWrapper(Long resumeId, Long jobId) {
        return new LambdaUpdateWrapper<ResumeAndJob>()
                .eq(ResumeAndJob::getResumeId, resumeId)
                .eq(ResumeAndJob::getJobId, jobId)
                .set(ResumeAndJob::getStatus, 1);
    }

    private LambdaUpdateWrapper<ResumeAndJob> noPassUpdateWrapper(Long resumeId, Long jobId) {
        return new LambdaUpdateWrapper<ResumeAndJob>()
                .eq(ResumeAndJob::getResumeId, resumeId)
                .eq(ResumeAndJob::getJobId, jobId)
                .set(ResumeAndJob::getStatus, 2);
    }

    private LambdaQueryWrapper<ResumeAndJob> getResumeIdListByJobId(Long jobId) {
        return new LambdaQueryWrapper<ResumeAndJob>()
                .eq(ResumeAndJob::getJobId, jobId)
                .eq(ResumeAndJob::getStatus, 0)
                .select(ResumeAndJob::getResumeId);
    }
}
